package com.zhuiYun.project.api.sysUser.service.impl;

import com.zhuiYun.project.api.sysUser.entiy.SysUser;
import com.zhuiYun.project.api.sysUser.mapper.SysUserMapper;
import com.zhuiYun.project.api.sysUser.model.SysUserModel;
import com.zhuiYun.project.api.sysUser.service.SysUserService;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.BeanUtils;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import com.zhuiYun.project.util.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName SysUserServiceImpl
 * @Description 用户管理服务类 用于实现用户的增删改查 登录 注销
 * @Author zcy
 * @Date 2023/3/24 16:15
 **/
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    /**
     * 通过登录名获取用户信息
     * @param loginName
     * @return
     */
    @Override
    public SysUser getUserByLoginName(String loginName) {
        // 判断用户登录名是否为空
        if(ObjectUtils.isNotEmpty(loginName)){
            // 使用Example表达式查询登录人信息
            Example example = new Example(SysUser.class);
            example.createCriteria().andEqualTo("loginName",loginName);
            return sysUserMapper.selectOneByExample(example);
        }else{
            return null;
        }
    }

    /**
     * 更新登录成功后更新用户信息
     * @param sysUser
     */
    @Override
    public void updateUserByLogin(SysUser sysUser) {
        if(ObjectUtils.isNotEmpty(sysUser)&&ObjectUtils.isNotEmpty(sysUser.getId())){
            sysUserMapper.updateByPrimaryKeySelective(sysUser);
        }
    }
    /**
     *
     * 新增用户
     * @param sysUserModel
     * @return
     * @throws CommonException
     */
    @Override
    public CommonResult saveUser(SysUserModel sysUserModel) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(sysUserModel)){
            SysUser sysUser = new SysUser();
            // sysUserModel 对象拷贝成 sysUser
            BeanUtils.copy(sysUserModel, sysUser);
            // 设置密码
            sysUser.setPassword(bCryptPasswordEncoder.encode(sysUser.getPassword()));
            int i = sysUserMapper.insert(sysUser);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /**
     * 修改用户信息
     * @param sysUserModel
     * @return
     */
    @Override
    public CommonResult updateUser(SysUserModel sysUserModel) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(sysUserModel)){
            SysUser sysUser = new SysUser();
            // sysUserModel 对象拷贝成 sysUser
            BeanUtils.copy(sysUserModel, sysUser);
            int i = sysUserMapper.updateByPrimaryKeySelective(sysUser);
            if(i>0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public CommonResult delUserById(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = sysUserMapper.deleteByPrimaryKey(id);
            // 判断是否操作成功
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * 分页查询
     * @param sysUserModel
     * @param pageRequest
     * @return
     */
    @Override
    public CommonResult getUserByPage(SysUserModel sysUserModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(SysUser.class);
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(sysUserModel.getUserName())){
            example.createCriteria().andLike("userName","%"+sysUserModel.getUserName()+"%");
        }
        PageResult pageResult = MybatisPageHelper.getPageResult(sysUserMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }
}
